package ThreadPool.lagou.concurrent.completableFuture.demo01;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @param
 * @author oyz
 * @version 1.0.4
 * @description: 对计算结果进行处理
 * @date 2022/10/7 16:18
 */
public class CompletetableFutureAPI2Demo {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        CompletableFuture.supplyAsync(()->{
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return 1;
        },executorService).thenApply(f->{
            return f+2;
        }).thenApply(f->{
            return f+2;
        }).whenComplete((v,e)->{
            if(e==null){
                System.out.println("计算结果为"+v);
            }
        }).exceptionally(e->{
            System.out.println(e);
            return null;
        });
        System.out.println(Thread.currentThread().getName()+"主线程先去忙其他的业务");
        executorService.shutdown();
    }
}

